# encoding: utf-8
def my_abs(x):
	if not isinstance(x, (int,float)):#isinstance 判断类型，是否为给出类型
		raise TypeError(" error")
	if x >=0:
		return x
	else:
		return -x
print my_abs(259)

#空函数 pass 只是作为语句占位符
#raise 用于抛出异常

#返回多个值 实际上返回一个tuple
def getUsefulInfo():
	return "Bob","Trader",25
a,b,c = getUsefulInfo()
print c
#无返回值的函数返回None

#函数的默认参数
def power(x,n=2):
	temp = 1
	while n>0:
		temp=temp*x
		n=n-1
	return temp

print power(5)
print power(5,4),power(5,n=4)

#默认参数必须指向不变对象,
def setEnd(lis = []):
	lis.append('End')
	return lis
print setEnd() #['End']
print setEnd() #['End', 'End']

# 可变参数 params
def addAll(*nambers):
	temp = 0
	for item in nambers:
		temp = temp + item
	return temp

print addAll(2,3,4,7)

lis = [2,3,4,7]
print addAll(*lis)

#关键字参数 内部建立了一个dict
def addPerson(name,age,**cust):
	print "name:",name,"age:",age,"customs:",cust
addPerson("Bod",90)
addPerson("will",10,sex='male',height=120)

#递归函数 
def fact(n):
	if n== 1:
		return 1
	else :
		return n *fact(n-1)

print fact(5)

#函数嵌套